package cn.jwcat.utils;

import cn.jwcat.entity.Staff;
import cn.jwcat.entity.User;
import cn.jwcat.utils.result.Result;

import java.util.Random;

public class otherUtils {
	public static Staff delStaff(Staff staff) {
		String idCard = staff.getIdCard();
		if (idCard == null) {
			staff.setPassword(null);
			return staff;
		}
		if (idCard.length() == 18) {
			idCard = idCard.replaceAll("(\\w{6})\\w*(\\w{3})", "$1*********$2");
		}
		staff.setIdCard(idCard);
		staff.setPassword(null);
		return staff;
	}

	public static String getRandomString(int length) {
		String base = "abcdefghijklmnopqrstuvwxyz0123456789";
		Random random = new Random();
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}

	// TODO: 2023-4-13 封装返回的请求，使数据操作的响应多样化
	public static Result dealBool(Boolean bool) {
		if (bool) return Result.success();
		return Result.error();
	}

	public static User dealUser(User user) {
		String idCard = user.getIdCard();
		if (idCard == null) {
			user.setPassword(null);
			return user;
		}
		if (idCard.length() == 18) {
			idCard = idCard.replaceAll("(\\w{6})\\w*(\\w{3})", "$1*********$2");
		}
		user.setIdCard(idCard);
		user.setPassword(null);
		return user;
	}
}
